<style>
#orchardStatus td {
	text-align:left;
}
</style>
<script>
$(function() {
	$('table#treetable').tablesorter();
});
</script>
<?php
    $vSettings = array();
    $vSettings = AFT_load('settings');
	global $vWorldTime, $vServertime;

	$object_list = load_botarray(F("objects.txt"));

	$numberorc=0;		
	$numbersorc=0;		
	$numberripe=0;	

	$countripe = 0;
	
	$numberarb = 0;
	$numberripearb = 0;
	
	foreach($object_list as $orchard) {
		$orcClass = $orchard['className'];
		$orcState = $orchard['state'];
		if ($orcClass == 'OrchardFeatureBuilding' || $orcClass == 'OrchardBuilding') {
			if ($orcState == 'ripe')  $numberripe++;
			$plant = date("Y-m-d H:i:s",$orchard['plantTime']/1000);
			$aoeplant = date("Y-m-d H:i:s",$orchard['aoePlantTime']/1000);
			$done = time()-($orchard['plantTime']/1000);

			if (stristr($orchard['itemName'],"superorchard")) {
				$numbersorc++;
				$ripetime = 23;
				if ($orcState == 'ripe')  $countripe+=39;
			} else {
				$numberorc++;
				$ripetime = 46;
				if ($orcState == 'ripe')  $countripe+=19;
			}
		
			$percent = ceil(100* ($done/($ripetime*60*60)));
		} elseif ($orcClass == 'ArboretumBuilding') {
			$numberarb++;
			if ($orcState == 'ripe') {
				$numberripearb++;
				$countripe+=19;
			}
		} else {
		}
	}

    $vSettings = array();
    $vSettings = AFT_load('settings');
	$vshrvorc='';
	if ($vSettings['harvestorc']==true) $vshrvorc=' checked';
?>	
<div class="maintable">
<table class="grid" id="treetable">
	<caption>
		Tree Breeding 
		<p>
		Sort by
		<select name="sort" size="1" onchange="fAction('faction=actionsort&actn=',this.value)" >
			<option value="realname" <?php if ($vSettings['sort']=='realname') echo ' selected';?>>Name</option>
			<option value="mastery" <?php if ($vSettings['sort']=='mastery') echo ' selected';?>>Mastery</option>
			<option value="coins" <?php if ($vSettings['sort']=='coins') echo ' selected';?>>Coins</option>
			<option value="cocos" <?php if ($vSettings['sort']=='cocos') echo ' selected';?>>Coconuts</option>
			<option value="date" <?php if ($vSettings['sort']=='date') echo ' selected';?>>Date</option>
			<option value="cnt" <?php if ($vSettings['sort']=='cnt') echo ' selected';?>>Count</option>
		</select>
		<select name="orderby" size="1" onchange="fAction('faction=actionorderby&actn=',this.value)" >
			<option value="asc" <?php if ($vSettings['orderby']=='asc') echo ' selected';?>>Asc</option>
			<option value="desc" <?php if ($vSettings['orderby']=='desc') echo ' selected';?>>Desc</option>
		</select>
		<input type="button" value="Sort" onClick="history.go(0)"/>
		</p>
		</caption>
			<thead>
				<tr>
					<th width="60">Image</th>
					<th>Name</th>
					<th>Mastery</th>
					<th class="sorttable_numeric">Breeds needed</th>
					<th>Release</th>
					<th>Coins</th>
					<th>Cocos</th>
					<th>Jade</th>
					<th>Count</th>
					<th>Action</th>
				</tr>
			</thead>
			<tbody>
<?php
$vAllTrees = AFT_MakeTreeDB('');
$mastery_counters  =  @unserialize(@file_get_contents(F('cropmastery.txt')));
$mastery_levels    =  @unserialize(@file_get_contents(F('cropmasterycount.txt')));

$vSections = array();
$vSections = AFT_load('sections');

if (strlen($vSettings['sort'])==0) $vSettings['sort']='realname';

foreach ($vAllTrees as $vATr) {
	$vatmp[]=$vATr[$vSettings['sort']];
}

if ($vSettings['orderby']=='desc') {
	array_multisort($vatmp, SORT_DESC, $vAllTrees); 
} else {
	array_multisort($vatmp, SORT_ASC, $vAllTrees); 
}

$hiddenTrees = 0;
$minBreeds = 0;
foreach ($vAllTrees as $vATr) {
	if ($vATr['code'] && $vATr['cnt']>0) {

		if ($vSettings['breederHide'] && $vATr['mastery'] >= 1 && $vATr['actionBred']!='forced' && $vATr['actionBred'] != 'tocnt') {
			$hiddenTrees ++;
			$hiddenTreeCount += $vATr['cnt'];
			continue;
		}

		if ($vATr['mastery'] < 1) $numtrees += $vATr['cnt'];
		
		
		$num = $vATr['cnt'];
		if ($num > 19) $num = 19;
		$c = ceil(($vATr['masmax']-$vATr['mascnt'])/$num);
		if ($c <= 0) $c = 0;
		if ($c > $minBreeds) $minBreeds = $c;
		$totalneeded += $c;
		$unmasteredTypes ++;
		$class = $vATr['mastery'] >= 1?"mastered":"";
		echo "<tr class=\"$class\">";
		echo AFT_tr($vATr,array('icon','name','mastery','orcsNeeded','release','coins','cocos','jade','count'));
		echo '<td>';
		echo '<table class="blind"><tbody><tr>';
		echo '<td>Action</td>';
?>
<td><select name="actn" size="1" onchange="fAction('faction=actionBred&name=<?=$vATr['name']?>&actn=',this.value)" >
<?php
$vslct='';
if ($vATr['actionBred']=='select') $vslct=' selected';
echo '<option value="select"'.$vslct.'>&nbsp;</option>';
$vslct='';
if ($vATr['actionBred']=='forced') $vslct=' selected';
echo '<option value="forced"'.$vslct.'>Forced</option>';
$vslct='';
if ($vATr['actionBred']=='tomast') $vslct=' selected';
echo '<option value="tomast"'.$vslct.'>To Mastery</option>';
$vslct='';
if ($vATr['actionBred']=='tocnt') $vslct=' selected';
echo '<option value="tocnt"'.$vslct.'>To Count</option>';
$vslct='';
if ($vATr['actionBred']=='spare') $vslct=' selected';
echo '<option value="spare"'.$vslct.'>Spare</option>';
echo '</select></td></tr><tr>';
echo '<td>Count</td>';
?>
<td><input maxlength="6" name="keep" size="3" type="text" value="<?=$vATr['actionBredcnt']?>"  
onchange="fAction('faction=actionBredcnt&name=<?=$vATr['name']?>&keep=',this.value)" ></td>
<?php
		echo '</tr></tbody></table>';
		echo '</td></tr>';
	}
}

	$breedsNeeded = ceil($totalneeded / 19);
	if ($minBreeds > $breedsNeeded) $breedsNeeded = $minBreeds;

?>

	</tbody>
	<tfoot>
		<tr>
			<th><?=$unmasteredTypes?></th>
			<th>Total&nbsp;unmastered</th>
			<th><?=$totalneeded?></th>
			<th><?=$breedsNeeded?></th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th><?=$numtrees?></th>
			<th>&nbsp;</th>
		</tr>
<?php if ($hiddenTrees) { ?>
		<tr>
			<th><?=$hiddenTrees?></th>
			<th>Hidden trees (mastered)</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th>&nbsp;</th>
			<th><?=$hiddenTreeCount?></th>
			<th>&nbsp;</th>
		</tr>

<?php } ?>
	</tfoot>
</table>
</div>
<div class="settings" >
	<div style="padding:4px;border-bottom:1px solid #303030;">
		<input type="checkbox" class="saveConf" name="settings-harvestorc" <?=$vSettings['harvestorc']?" checked=\"checked\"":""?>> Harvest non-breeding orchards?<br>
		<input type="checkbox" class="saveConf" name="settings-breederHide" <?=$vSettings['breederHide']?" checked=\"checked\"":""?>> Show only unmastered and forced/to-count trees<br>
		Harvest/breed max. <input type="text" class="saveConf" style="width:20px;" name="settings-maxHarvestPerCycle" value="<?=$vSettings['maxHarvestPerCycle']?>"> orchards/groves per cycle<br>
		Spend max. <input type="text" class="saveConf" style="width:20px;" name="settings-maxBreedTimePerCycle" value="<?=$vSettings['maxBreedTimePerCycle']?>"> minutes breeding per cycle<br>
		Do init (reload) after <input type="text" class="saveConf" style="width:20px;" name="settings-doInitAfterMinBreeding" value="<?=$vSettings['doInitAfterMinBreeding']?>"> minutes of breeding (0 to disable)
		<br>
		Breeding order: 
		<select name="settings-breedingOrder" size="1" class="saveConf">
		<?php foreach (array('realname', 'mastery', 'coins', 'cocos', 'date', 'cnt') as $k) { 
			echo "<option value=\"$k\"". ($vSettings['breedingOrder']==$k?' selected':'').">$k</option>\n";
		} ?>
		</select>
		<select name="settings-breedingOrderDir" size="1" class="saveConf">
			<option value="asc" <?= $vSettings['breedingOrderDir']=='asc'?' selected':''?>>ascending</option>
			<option value="desc" <?= $vSettings['breedingOrderDir']=='desc'?' selected':''?>>descending</option>
		</select>
		<br>
		<input type="checkbox" class="saveConf" name="settings-breedingResort" <?=$vSettings['breedingResort']?" checked=\"checked\"":""?>> re-sort order between breeds?<br>

		
		
	</div>
	
	<?php
	echo ($numberripe+$numberripearb).' of  '.($numberorc+$numbersorc+$numberarb).' orchards/groves are ripe ';
	printf("(%u points &asymp; %0.1f masteries). ",$countripe,$countripe/450);
	echo "Your current breeding power is ".sprintf("%u",($numberorc+$numberarb)*19/2 + ($numbersorc*39)).' mastery points &asymp; '.sprintf("%0.1f",(($numberorc+$numberarb)*19/2 + ($numbersorc*39))/450).' tree masteries per day (23h).<br>';
	?>
	<table class="grid" id="orchardStatus">
		<caption>Orchards ripening progress</caption>
		<tr><th>Progress</th><th style="width:100px;">Orchards</th><th style="width:100px;">Super Orchards</th><th style="width:100px;">Groves</th></tr>
		<tr><td style="text-align:right;">0%</td><td><?php echo AllForTrees_OProgress("O",0,0); ?></td><td><?php echo AllForTrees_OProgress("SO",0,0); ?></td><td><?php echo AllForTrees_OProgress("G",0,0); ?></td></tr>
		<tr><td style="text-align:right;">1&hellip;9%</td><td><?php echo AllForTrees_OProgress("O",0.01,0.1); ?></td><td><?php echo AllForTrees_OProgress("SO",0.01,0.09); ?></td><td><?php echo AllForTrees_OProgress("G",0.01,0.09); ?></td></tr>
		<tr><td style="text-align:right;">10&hellip;29%</td><td><?php echo AllForTrees_OProgress("O",0.1,0.3); ?></td><td><?php echo AllForTrees_OProgress("SO",0.1,0.29); ?></td><td><?php echo AllForTrees_OProgress("G",0.1,0.29); ?></td></tr>
		<tr><td style="text-align:right;">30&hellip;49%</td><td><?php echo AllForTrees_OProgress("O",0.3,0.5); ?></td><td><?php echo AllForTrees_OProgress("SO",0.3,0.49); ?></td><td><?php echo AllForTrees_OProgress("G",0.3,0.49); ?></td></tr>
		<tr><td style="text-align:right;">50&hellip;79%</td><td><?php echo AllForTrees_OProgress("O",0.5,0.8); ?></td><td><?php echo AllForTrees_OProgress("SO",0.5,0.79); ?></td><td><?php echo AllForTrees_OProgress("G",0.5,0.79); ?></td></tr>
		<tr><td style="text-align:right;">80&hellip;89%</td><td><?php echo AllForTrees_OProgress("O",0.8,0.9); ?></td><td><?php echo AllForTrees_OProgress("SO",0.8,0.89); ?></td><td><?php echo AllForTrees_OProgress("G",0.8,0.89); ?></td></tr>
		<tr><td style="text-align:right;">90&hellip;99%</td><td><?php echo AllForTrees_OProgress("O",0.9,1); ?></td><td><?php echo AllForTrees_OProgress("SO",0.9,0.99); ?></td><td><?php echo AllForTrees_OProgress("G",0.9,0.99); ?></td></tr>
		<tr style="font-weight:bold;"><td style="text-align:right;">100%</td><td><?php echo AllForTrees_OProgress("O",1,1); ?></td><td><?php echo AllForTrees_OProgress("SO",1,1); ?></td><td><?php echo AllForTrees_OProgress("G",1,1); ?></td></tr>
		<tr><td style="text-align:right;">total</td><td><?=$numberorc?></td><td><?=$numbersorc?></td><td><?=$numberarb?></td></tr>
	</table>
	<div style="color:red;">This table may be inaccurate!</div>


</div>
